package team.knowers.qzh.qzhgrame.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import team.knowers.qzh.qzhgrame.dao.entity.SectionDO;
import team.knowers.qzh.qzhgrame.dao.mapper.SectionMapper;
import team.knowers.qzh.qzhgrame.dto.req.*;
import team.knowers.qzh.qzhgrame.dto.resp.AllSectionRespDTO;
import team.knowers.qzh.qzhgrame.dto.resp.SectionDetailsRespDTO;
import team.knowers.qzh.qzhgrame.dto.resp.SectionPageRespDTO;
import team.knowers.qzh.qzhgrame.service.SectionGrameService;


import java.util.ArrayList;
import java.util.List;

import static team.knowers.qzh.qzhgrame.common.web.FourteenDigitRandomGenerator.generateFourteenDigitRandom;

@Service
@AllArgsConstructor
public class SectionGrameServiceImpl extends ServiceImpl<SectionMapper, SectionDO> implements SectionGrameService {
    @Override
    public int updateCourseContent(UpdateSectionReqDTO req) {
        LambdaQueryWrapper<SectionDO> queryWrapper = Wrappers.lambdaQuery(SectionDO.class)
                .eq(SectionDO::getId, req.getId())
                .eq(SectionDO::getChapterId, req.getChapterId());
        SectionDO sectionDO = baseMapper.selectOne(queryWrapper);
        int version = sectionDO.getVersion();
        sectionDO.setVersion(version);
        sectionDO.setName(req.getName());
        sectionDO.setOrder(req.getOrder());
        sectionDO.setAuthorName(req.getAuthorName());
        return baseMapper.update(sectionDO, queryWrapper);
    }

    @Override
    public IPage<SectionPageRespDTO> pageSection(PageSectionReqDTO req) {
        LambdaQueryWrapper<SectionDO> queryWrapper = Wrappers.lambdaQuery(SectionDO.class).eq(SectionDO::getChapterId, req)
                .eq(SectionDO::getDelFlag, 0)
                .orderByAsc(SectionDO::getCreateTime);
        IPage<SectionDO> sectionDOIPage = baseMapper.selectPage(req, queryWrapper);
        return sectionDOIPage.convert(each -> BeanUtil.toBean(each, SectionPageRespDTO.class));
    }

    /**
     * 创建章节
     */
    @Override
    public int addSection(AddSectionReqDTO req) {
        SectionDO build = SectionDO.builder()
                .id(generateFourteenDigitRandom())
                .name(req.getName())
                .order(req.getOrder())
                .sectionNumber(generateFourteenDigitRandom())
                .likes(0)
                .authorId(req.getAuthorId())
                .authorName(req.getAuthorName())
                .chapterId(req.getChapterId())
                .version(0).build();
        build.setDelFlag(0);
        return baseMapper.insert(build);
    }

    /**
     * 删除章节/版本
     */
    @Override
    public int deleteSection(Long id) {
        LambdaQueryWrapper<SectionDO> queryWrapper = Wrappers.lambdaQuery(SectionDO.class).eq(SectionDO::getId, id)
                .eq(SectionDO::getDelFlag, 0);
        SectionDO sectionDO = baseMapper.selectOne(queryWrapper);
        sectionDO.setDelFlag(1);
        return baseMapper.update(sectionDO, queryWrapper);
    }

    @Override
    public List<AllSectionRespDTO> showSection(Long id, Long authorId) {
        LambdaQueryWrapper<SectionDO> queryWrapper = Wrappers.lambdaQuery(SectionDO.class)
                .eq(SectionDO::getAuthorId, authorId)
                .eq(SectionDO::getChapterId, id)
                .eq(SectionDO::getDelFlag, 0)
                .orderByAsc(SectionDO::getOrder);

        List<SectionDO> sectionDOS = baseMapper.selectList(queryWrapper);
        List<AllSectionRespDTO> sectionDTOs = new ArrayList<>();

        for (SectionDO sectionDO : sectionDOS) {
            AllSectionRespDTO sectionDTO = new AllSectionRespDTO();
            sectionDTO.setOrder(sectionDO.getOrder());
            sectionDTO.setId(sectionDO.getId());
            sectionDTO.setSectionNumber(sectionDO.getSectionNumber());
            sectionDTO.setName(sectionDO.getName());
            sectionDTOs.add(sectionDTO);
        }

        return sectionDTOs;
    }

    /**
     * 创建版本
     */
    @Override
    public int addVersion(AddVersionReqDTO req) {
        LambdaQueryWrapper<SectionDO> eq = Wrappers.lambdaQuery(SectionDO.class).eq(SectionDO::getDelFlag, 0).eq(SectionDO::getSectionNumber, req.getSectionNumber());
        SectionDO one = baseMapper.selectOne(eq);
        SectionDO build = SectionDO.builder()
                .sectionNumber(req.getSectionNumber())
                .id(generateFourteenDigitRandom())
                .authorId(req.getAuthorId())
                .authorName(req.getAuthorName())
                .order(one.getOrder())
                .likes(0)
                .chapterId(one.getChapterId())
                .version(1)
                .name(one.getName()).build();
        return baseMapper.insert(build);
    }

    /**
     * 展示某人创作所有版本
     * @param id
     * @return
     */
    @Override
    public List<SectionDO> listOnesVersion(Long id) {
        LambdaQueryWrapper<SectionDO> gt = Wrappers.lambdaQuery(SectionDO.class).eq(SectionDO::getDelFlag, 0).eq(SectionDO::getAuthorId, id).gt(SectionDO::getVersion, 0);
        List<SectionDO> sectionDOS = baseMapper.selectList(gt);
        return sectionDOS;
    }
    /**
     * 作者原创的章节的目录
     */
    @Override
    public List<SectionDetailsRespDTO> getSectionDetailsById(Long chapterId) {
        LambdaQueryWrapper<SectionDO> queryWrapper = Wrappers.lambdaQuery(SectionDO.class)
                .eq(SectionDO::getChapterId, chapterId)
                .eq(SectionDO::getDelFlag, 0)
                .eq(SectionDO::getVersion, 0)
                .orderByAsc(SectionDO::getOrder);
        List<SectionDO> sectionDO = baseMapper.selectList(queryWrapper);
        List<SectionDetailsRespDTO> results = new ArrayList<>();
        for (SectionDO sectionDO1 : sectionDO) {
            results.add(BeanUtil.toBean(sectionDO1, SectionDetailsRespDTO.class));
        }
        return results;
    }

}
